package com.czk.bootlettuce.controller;

import com.czk.bootlettuce.utils.SeckillUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.UUID;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/17 16:57
 */
@RestController
@RequestMapping("/redis")
public class SecKillController {
    @Autowired
    private SeckillUtil seckillUtil;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/kill")
    public String skill() {
        String uid = UUID.randomUUID().toString().replaceAll("-", "");
        String prodid = "goods";
        boolean b = seckillUtil.soSecKill(uid, prodid);
        if (b) {
            return "秒杀成功";
        } else {
            return "秒杀失败";
        }
    }

    @PostConstruct
    public void init() {
        String kcKey = "sk:goods:qt";
        redisTemplate.opsForValue().set(kcKey, "10");
    }
}
